<?php
$up = '';
$msg = '';

if(isset($_POST['photoid_submit']) && $_POST['photoid_submit'] == 'Done')
{
		
	if(isset($_SESSION['saved_files']) && !empty($_SESSION['saved_files']))
	{
		
		foreach($_SESSION['saved_files'] as $files)
		{
			
			
			$sql = "INSERT INTO crm_idcheckhistory(`app_id`,`CurrentContact_id`,`OriginalContact_id`,`CreatedDate`,`IDCheckFile`,`IDCheckFilePath`,`IDCheckNo`,`IDCheckReferNo`,`Createdby`) VALUES('".$_SESSION['app_id']."', '".$_SESSION['CurrentContact_id']."' ,'".$_SESSION['CurrentContact_id']."' ,'".getDateFormat($files['created_time'], '')."' ,'".$files['file']."', '".$files['stored_path']."', '0', '0', '".$_SESSION['CurrentContact_id']."')";
				mysql_query($sql);
				$IDCheckhistoryID = mysql_insert_id();
				if($IDCheckhistoryID > 0)	
				{
					
					
					$action = "IDVerificationUpdate";
					$description = "Client update the ID verification document";
					saveActivityHistory($action, $description);
					$msg = "The files has been uploaded successfully, Thank you";
					$up = 1;
					//echo 'Success';
				}
				else
				{
					if(file_exists($file_path)) 
					{
						unlink($file_path);
					}					
					//echo 'Failure';
				}
		}
		
		if($up){/*
			$sql = "UPDATE sacc_verificationstatus SET idcheck_status = '1' WHERE app_id = ".$_SESSION['app_id'];
			if(mysql_query($sql)){
			$msg = "The files has been uploaded successfully, Thank you";
			unset($_SESSION['saved_files']); 
						   }
		*/}
		
	}
}
?>
<script type="text/javascript">
		<?php $timestamp = time();?> 
		$(function() {
				   
				   $('input[type="file"]').attr(
					'accept',
					'image/gif, image/jpeg, image/jpg, image/png, .gif, .jpeg, .jpg, .png, .doc, .docx, .pdf'
					);
				   
			$('#file_upload').uploadifive({
				'auto'         : true,
				'dnd' 		   : true, 	
				'queueID'      : 'queue',
				'formData'     : {
					'timestamp' : '<?php echo $timestamp;?>',
					'token'     : '<?php echo md5('unique_salt' . $timestamp);?>',
					'type'      : 'photoidcheck'					
				},
				'uploadLimit'  : 1,
				'queueSizeLimit' : 1,
				'buttonText'   : 'Browse Files ...',
				'uploadScript' : 'uploadifive.php',
				'fileTypeDesc' : 'Image,Pdf and Document Files are alowed',
				//'fileType' : 'image/png|image/jpg|image/gif|image/jpeg',
				'fileSizeLimit' : '2MB',
				'onError'      : function(errorType) {
				if(errorType=='FILE_SIZE_LIMIT_EXCEEDED')
				{
					//alert('Your File size should not exceed 10 MB');
					$('#notify').html('Your File size should not exceed 2 MB, Try again');
				}
					
            
        },
				 'onUploadComplete' : function(file, data) {
				//alert(data);
					if($.trim(data) == 'Success'){
						
						$('#notify').html('You File has been queued for upload');
                         	//window.location.href ='index.php?dashboard';												
					}else{
						
						if($.trim(data) == 'Invalid')
						{
							$('#notify').html('You are not allowed to upload this kind of files, Try again');
						}
						//$('#notify').html(data+' Try again');
						//$('#notify').html('Your File size should not exceed 10 MB, Try again');
					}
    },
	
	'width'    : 210,
	'onCancel'     : function() {
		
		deleteFile(file.name)
		
	}
	
			});
		});
		
		
		function deleteFile(filename)
		{
			var request = $.ajax({
			url: "uploadifive.php",
			type: "POST",
			data: { type : 'photoiddelete', filename : filename },
			dataType: "html"
			});
			
			request.done(function( msg ) {
			//alert('The file ' + filename + ' was cancelled!');
			$('#notify').html(msg);
			});
			
			request.fail(function( jqXHR, textStatus ) {
			$('#notify').html( "Request failed: " + textStatus );
			});
		}
	</script>
 <img src="images/employe-mony..gif" />
            <h2><i class="fa fa-arrow-circle-right"></i>Photo ID</h2>
            <div class="well emp-detail span12">
            <p>For identification purpose, please provide us a copy of your driver licence or passport.</p>
                   <div class="span5 payslip">
                       <img src="images/licence.gif"/><br /><br />
                       <img src="images/passport.gif"/>
                       <?php 
	  if($_SESSION['app_id'])
	  {
		  $app_id = $_SESSION['app_id'];
		  $row = getVerificationStatus($app_id);
	  }
	  //print_r($row); exit; 
	   $idcheck_status = $row['idcheck_status'];
	   
	//echo  $idcheck_status ;
	
	//exit; 
	
	  ?>
                   </div>
                   <div class="span6 event <?php if(!empty($idcheck_status)){ echo 'disabled';} ?>">
                    <h5>Method</h5> 
                     
                       <p><a id="fileupload" href="" style="padding-left:70px; padding-right:77px;">Upload Files</a></p><br />
                       <p> <a href="">Email:Loans@maxcash.com.au</a></p><br />
                       <p><a href=""  style="padding-left:58px; padding-right:60px;">Fax:02 8005 3976</a></p>
                        <div class="span 6 successmsg">
                  <?php if( !empty($msg)): ?>
        <span>The files has upload successfully, thank you</span>
          <?php endif; ?>
          </div>
                  </div>
                  <button id="next" class="btn btn-green pull-right" type="button">Done</button>
                  <!--<div class=" span6 emp-detail disabled">
                   <h5>Method</h5> 
                     
                       <p><a id="fileupload" href="" style="padding-left:70px; padding-right:77px;">Upload Files</a></p><br />
                       <p> <a href="">Email:Loans@maxcash.com.au</a></p><br />
                       <p><a href=""  style="padding-left:65px; padding-right:60px;">Fx:XX XXXX XXXX</a></p>
            </div>
            -->
            
            </div>
            
            <div id="sendfile" style="display:none;">
            <div id="autopopulate">
                <form name="verifyphotoid" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?verify-photo-id" enctype="multipart/form-data">
                    <input type="file" name="file_upload" id="file_upload"  />
                    <div id="queue"></div>    
                    <div id="notify"></div> 
                    <input type="button" name="cancel" id="cancel" value="Cancel" onclick="closeUploadDialog();" class="btn btn-green pull-left" />          
                    <input type="submit" name="photoid_submit" value="Done" class="btn btn-green pull-right" />
                </form>
      </div>      </div>
</div>

<script>  


function closeUploadDialog()
{
	$('#sendfile').dialog('close');	
	return true;
}

    $(document).ready(function(){
							   
	$('.disabled').block({ message: null,overlayCSS: {backgroundColor:'transparent',cursor:'default' } }); 

	$('#sendfile').dialog({width:293,title:'Upload Files',autoOpen:false,modal:true});
	
	$('#fileupload').on('click',function(){
										 	
	$('#sendfile').dialog('open');	
	return false;
	
	});	
	
	$('#next').on('click', function(){
									
		window.location = 'index.php?dashboard'; 
	});
	
	
	
	
});
</script>